package com.ruoyi.report_hntjg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.report_hntjg.domain.bo.ReportHntjgGjbhcjlbBaDetailBo;
import com.ruoyi.report_hntjg.mapper.ReportHntjgGjbhcjlbBaDetailMapper;
import com.ruoyi.report_hntjg.service.IReportHntjgGjbhcjlbBaDetailService;
import com.ruoyi.report_hntjg.domain.ReportHntjgGjbhcjlbBaDetail;
import com.ruoyi.report_hntjg.domain.vo.ReportHntjgGjbhcjlbBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 钢筋位置及保护层厚度试验检测记录-详情Service业务层处理
 *
 * @author tll
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
public class ReportHntjgGjbhcjlbBaDetailServiceImpl implements IReportHntjgGjbhcjlbBaDetailService {

    private final ReportHntjgGjbhcjlbBaDetailMapper baseMapper;

    /**
     * 查询钢筋位置及保护层厚度试验检测记录-详情
     */
    @Override
    public ReportHntjgGjbhcjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询钢筋位置及保护层厚度试验检测记录-详情列表
     */
    @Override
    public TableDataInfo<ReportHntjgGjbhcjlbBaDetailVo> queryPageList(ReportHntjgGjbhcjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportHntjgGjbhcjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋位置及保护层厚度试验检测记录-详情列表
     */
    @Override
    public List<ReportHntjgGjbhcjlbBaDetailVo> queryList(ReportHntjgGjbhcjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail> buildQueryWrapper(ReportHntjgGjbhcjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportHntjgGjbhcjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportHntjgGjbhcjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShicz1() != null, ReportHntjgGjbhcjlbBaDetail::getShicz1, bo.getShicz1());
        lqw.eq(bo.getShicz2() != null, ReportHntjgGjbhcjlbBaDetail::getShicz2, bo.getShicz2());
        return lqw;
    }

    /**
     * 新增钢筋位置及保护层厚度试验检测记录-详情
     */
    @Override
    public Boolean insertByBo(ReportHntjgGjbhcjlbBaDetailBo bo) {
        ReportHntjgGjbhcjlbBaDetail add = BeanUtil.toBean(bo, ReportHntjgGjbhcjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改钢筋位置及保护层厚度试验检测记录-详情
     */
    @Override
    public Boolean updateByBo(ReportHntjgGjbhcjlbBaDetailBo bo) {
        ReportHntjgGjbhcjlbBaDetail update = BeanUtil.toBean(bo, ReportHntjgGjbhcjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportHntjgGjbhcjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋位置及保护层厚度试验检测记录-详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
